import { defineStore } from 'pinia';
import { ref } from 'vue';
import request from '@/utils/request';
import { showNotification } from '@/utils/notify';
import type { Menu ,RoleMenu,DoAssignParams} from '@/types/menu';



type MyRequest = {
  code: number;
  message: string;
  data: Menu[];
};

export const useRoleMenuStore = defineStore('role-menu', () => {
  // 分别存储三种角色的菜单树状数据
  const adminRoleMenus = ref<Menu[]>([]);
  const stuRoleMenus = ref<Menu[]>([]);
  const teachRoleMenus = ref<Menu[]>([]);

  // 用于存储菜单树状结构数据
  const menuTreeData = ref<Menu[]>([]);



  // 获取菜单树状结构数据，传入角色编码
  const fetchTreeRoleMenus = async (roleCode: string) => {
    try {
      const url = `api/sys-menu/toAssign/${roleCode}`;
      const { code, data, message } = await request.get<MyRequest>(url);
      if (code === 200) {
        switch (roleCode) {
          case 'ADMIN':
            adminRoleMenus.value = data;
            break;
          case 'STU':
            stuRoleMenus.value = data;
            break;
          case 'TEACH':
            teachRoleMenus.value = data;
            break;
          default:
            break;
        }
      } else {
        showNotification(message, false);
      }
    } catch (error) {
      showNotification('获取角色树状菜单数据失败', true);
    }
  };

  const fetchRoleMenus = async (roleCode:string)=>{
    try {
      const url = `api/sys-menu/toAssign/${roleCode}`;
      const { code, data, message } = await request.get<MyRequest>(url);
      if (code == 200) {

        return data
        
      } else {
        showNotification(message, false);
      }
    } catch (error) {
      showNotification('获取角色树状菜单数据失败', true);
    }
  }

  // 添加角色菜单
  const doAssign = async (params: DoAssignParams) => {
    try {
      const { code, message } = await request.post<{ code: number; message: string }>('/api/sys-menu/doAssign', params);
      if (code === 200) {
        showNotification('权限分配成功');
        
        await fetchTreeRoleMenus(params.roleCode as string);
      } else {
        showNotification(message, true);
      }
    } catch (error) {
      showNotification('角色菜单添加失败', true);
    }
  };

  // 更新角色菜单
  const updateRoleMenu = async (roleMenu: RoleMenu) => {
    try {
      const { code, message } = await request.put<{ code: number; message: string }>('api/sys-menu/update', roleMenu);
      if (code === 200) {
        showNotification('角色菜单更新成功');
        await fetchTreeRoleMenus(roleMenu.roleCode);
      } else {
        showNotification(message, true);
      }
    } catch (error) {
      showNotification('角色菜单更新失败', true);
    }
  };

  // 删除角色菜单
  const deleteRoleMenu = async (id: number, roleCode: string) => {
    try {
      const { code, message } = await request.delete<{ code: number; message: string }>(`api/sys-menu/delete/${id}`);
      if (code === 200) {
        showNotification('角色菜单删除成功');
        await fetchTreeRoleMenus(roleCode);
      } else {
        showNotification(message, true);
      }
    } catch (error) {
      showNotification('角色菜单删除失败', true);
    }
  };

  return {
    adminRoleMenus,
    stuRoleMenus,
    teachRoleMenus,
    menuTreeData,
    fetchRoleMenus,
    fetchTreeRoleMenus,
    doAssign,
    updateRoleMenu,
    deleteRoleMenu
  };
});